<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/7/7
 * Time: 19:17
 */

namespace Moblie\Controller;


use Face\Controller\CommonController;

class SendController extends CommonController
{
    /*发布广告*/
    public function index()
    {
        if(IS_POST){
            $post = i('post.');
            $verify = new \Think\Verify();
            $check = $verify->check($post['code']);
            if(!$check)
            {
                $this->error("验证码错误");
            }
            else
            {
                /*获取图片*/
                $img = $this->uploadimg();
                if(trim(empty($post['title']))){
                    $this->error('请输入标题');
                }
                if(trim(empty($post['key']))){
                    $this->error('请输入关键词');
                }
                if(trim(empty($post['des']))){
                    $this->error('请输入描述');
                }
                if(trim(empty($post['content']))){
                    $this->error('请输入内容');
                }
                $cityname = M('city')->where(['id'=>$post['city']])->find();
                $data = [
                    'title'=>$post['title'],
                    'key'=>$post['key'],
                    'des'=>$post['des'],
                    'time'=>date("Y-m-d H:i:s", time()),
                    'content'=>$post['content'],
                    'thumb'=>$img,
                    'check'=>0,
                    'read'=>42,
                    'city'=>$post['city'],
                    'sort'=>1,
                    'maker'=>$_SESSION['wechatname'],
                    'login'=>NULL,
                    'cityname'=>$cityname['name'],
                    'openid'=>$_SESSION['wechatopenid'],
                ];
                if(M('add')->add($data)){
                    $this->success('发布成功，请等待管理员审核',U('Send/check'));
                }else{
                    $this->error('失败，未知错误');
                }

            }

        }else{
            $id = ['2','11','88','89','6','7','8','90','91'];
            $where['id'] = ["in",$id];
            $data = M('city')->where($where)->select();
            $this->assign('data',$data);
            $this->display();
        }
    }
    /*上传图片*/
    public function uploadimg()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     1048576 ;// 设置附件上传大小 1M
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './data/'; // 设置附件上传根目录
        $upload->savePath  =     'upload/wechat/'; // 设置附件上传（子）目录
        $upload->saveName  =     time().rand(0,999);//设置上传文件名称
        // 上传文件
        $info   =   $upload->upload();
        $url = $info['thumb']['savepath'].$info['thumb']['savename'];
        if(!$url){
            $this->error('你忘了上传封面图啦~');
        }
        $this->makeimg($upload->rootPath.$url);
        $path = $info['thumb']['savepath'];
        $imgurl = substr($path,7);
        $imgurl = $imgurl.$info['thumb']['savename'];
        return $imgurl;
    }
    /*缩略图*/
    public function makeimg($img)
    {
        $image = new \Think\Image();
        $image->open($img);
        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
        $image->thumb(100, 100)->save($img);
    }
    /*验证码*/
    public function code()
    {
        $config =    array(
            'fontSize'    =>    30,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
            'codeSet'     =>    '0123456789' //数字
        );
        $Verify =     new \Think\Verify($config);
        $Verify->entry();
    }
    /*审核中信息*/
    public function check()
    {
        $count      = M('add')->where(['openid'=>$_SESSION['wechatopenid'],'check'=>0])->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,6);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme'," %FIRST% %UP_PAGE%  %DOWN_PAGE% %END%");
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('add')->where(['openid'=>$_SESSION['wechatopenid'],'check'=>0])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $k=>$v) {
            $list[$k]['city'] = M('city')->where(['id' => $v['city']])->field('name')->find();
        }

        $this->assign(['page'=>$show,'data'=>$list]);

        $this->display();
    }
    /*显示信息*/
    public function pass()
    {
        $count      = M('add')->where(['openid'=>$_SESSION['wechatopenid'],'check'=>1])->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,6);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme'," %FIRST% %UP_PAGE%  %DOWN_PAGE% %END%");
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('add')->where(['openid'=>$_SESSION['wechatopenid'],'check'=>1])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $k=>$v) {
            $list[$k]['city'] = M('city')->where(['id' => $v['city']])->field('name')->find();
        }

        $this->assign(['page'=>$show,'data'=>$list]);

        $this->display();
    }
}